import {
  hydrologyNamesQuery,
  rainfallCount,
  rainfallBaseLine
  } from 'api/hydrological';

  export default {
      namespace: 'hydrological',
      state: {
        rainfall: {
          xdata: ['201901', '201902', '201903', '201904', '201905', '201906','201907', '201908', '201909', '201910', '201911', '201912'],
          rainfall: [181, 210, 200, 230, 210, 260,230,222,180,198,120,111]
        },
        baseLine:{month_max:120,month_min:30,year_max:1200,year_min:360},
        indexType:[],
        indexContent:[]
      },

      effects: {
        *hydrologyNamesQuery({ payload }, { call, put }) {
          const result = yield call(hydrologyNamesQuery, payload)
          if (result && result.success) {
            yield put({
              type: 'setState',
              payload: {
                indexType: result.data,
              },
            })
          } else {
            throw data
          }
        },
        *hydrologyContentQuery({ payload }, { call, put }) {
          const result = yield call(hydrologyNamesQuery, payload)
          if (result && result.success) {
            yield put({
              type: 'setState',
              payload: {
                indexContent: result.data,
              },
            })
          } else {
            throw data
          }
        },
        *rainfallCount({ payload }, { call, put }) {
          const result = yield call(rainfallCount, payload)
          if (result && result.success) {
            yield put({
              type: 'setState',
              payload: {
                rainfall: result.data,
              },
            })
          } else {
            throw data
          }
        },
        *rainfallBaseLine({ payload }, { call, put }) {
          const result = yield call(rainfallBaseLine, payload)
          if (result && result.success) {
            let data = {};
            for(let item of result.data){
              switch(item.code) {
                case "m_max":
                  data['month_max'] = item.name;
                  break;
                case "m_min":
                  data['month_min'] = item.name;
                  break;
                case "y_max":
                  data['year_max'] = item.name;
                  break;
                case "y_min":
                  data['year_min'] = item.name;
                  break;
              }
            }
            yield put({
              type: 'setState',
              payload: {
                baseLine: data,
              },
            })
          } else {
            throw data
          }
        },
      },

      reducers: {
        setState(state, action) {
          return {
              ...state,
              ...action.payload
          };
        }
      },
    }
